
public class MyLinkList {
    // 1、无头单向非循环链表实现
    static class ListNode{
        public ListNode next;
        public int data;

        public ListNode(int val){
            this.data = val;
        }
    }
    public ListNode head;
    public boolean IndexOutOfBoundsException(int index){
        if(index<0){
            throw new IndexOutOfBoundsException("下标小于零，错误");
        }else{
            ListNode cur = head;
            int count = 0;
            while(cur!=null){
                count++;
                cur = cur.next;
            }
            if(index>=count){
                throw new IndexOutOfBoundsException("下标超过限制，错误");
            }
            return false;
        }
    }

    //头插法
    public void addFirst(int data){
        ListNode cur = new ListNode(data);
        cur.next = this.head;
        this.head = cur;
    }
    //尾插法
    public void addLast(int data){
        ListNode cur = head;
        while(cur.next!=null){
            cur = cur.next;
        }
        ListNode newnode = new ListNode(data);
        cur.next = newnode;
        newnode.next = null;
    }
    //任意位置插入,第一个数据节点为0号下标
    public void  addIndex(int index,int data){
        if(index==0){
            this.addFirst(data);
            return;
        }
        if(!IndexOutOfBoundsException(index)){
            ListNode newnode = new ListNode(data);
            ListNode cur = head;
            int count = index-1;
            while(count>0){
                cur = cur.next;
                count--;
            }
            newnode.next = cur.next;
            cur.next = newnode;

        }
    }
    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key){
        ListNode cur =this.head;
        while(cur!=null){
            if(cur.data==key){
                return true;
            }
            cur = cur.next;
        }
        return false;
    }
    //删除第一次出现关键字为key的节点
    public void remove(int key){
        ListNode cur = this.head;
        if(this.head.data==key) {
            this.head = this.head.next;
            return;
        }
        while(cur!=null){
            if(cur.next.data==key){
                cur.next = cur.next.next;
                return;
            }
            cur = cur.next;
        }


    }
    //删除所有值为key的节点
    public void removeAllKey(int key){
        ListNode cur = head;
        while(cur!=null&&this.head!=null){
            if(this.head.data==key){
                this.head = this.head.next;
                cur = this.head;
            }else if(cur.next.data==key){
                cur.next = cur.next.next;
                cur = cur.next;
            }
        }
    }
    //得到单链表的长度
    public int size(){
        int count = 0;
        ListNode cur = head;
        while(cur!=null){
            count++;
            cur = cur.next;
        }
        return count;
    }
    public void display(){
        ListNode cur = head;
        while(cur!=null){
            System.out.print(cur.data+"->");
            cur = cur.next;
        }
        System.out.println("null");
    }
    public void clear(){
        this.head = null;

    }

}
